/* 895. 最大频率栈 */
var FreqStack = function () {
	this.freq = new Map()
	this.group = new Map()
	this.maxFreq = 0
}

/**
 * @param {number} val
 * @return {void}
 */
FreqStack.prototype.push = function (val) {
	this.freq.set(val, (this.freq.get(val) || 0) + 1)
	// 分组 个数为1的一组 2的一组
	if (!this.group.has(this.freq.get(val))) {
		this.group.set(this.freq.get(val), [])
	}
	this.group.get(this.freq.get(val)).push(val)
	this.maxFreq = Math.max(this.maxFreq, this.freq.get(val))
}

/**
 * @return {number}
 */
FreqStack.prototype.pop = function () {
	const val = this.group.get(this.maxFreq)[
		this.group.get(this.maxFreq).length - 1
	]
	this.freq.set(val, this.freq.get(val) - 1)
	this.group.get(this.maxFreq).pop()
	if (this.group.get(this.maxFreq).length === 0) {
		this.maxFreq--
	}
	return val
}
const data = [5, 7, 5, 7, 4, 5]
const freqStack = new FreqStack()
data.forEach((item) => {
	freqStack.push(item)
})
console.log('freqStack :>>', freqStack.freq, freqStack.group)
for (let i = 0; i < 4; i++) {
	// console.log(i + ' :>>', freqStack.pop())
}
